//
//  ZYPBToken.m
//  ZYWebImage
//
//  Created by wangzhipan on 2025/4/27.
//

#import "ZYPBToken.h"
#import "ZYPBCore.h"
@interface ZYPBToken()
@property (nonatomic, strong, readwrite) Class reqCls;
@property (nonatomic, strong, readwrite) Class rspCls;
@property (nonatomic, assign, readwrite) uint32_t maxType;
@property (nonatomic, assign, readwrite) uint32_t minType;
@property (nonatomic, assign, readwrite) BOOL isCanceled;
@property (nonatomic, weak) ZYPBCore *core;
@end
@implementation ZYPBToken
- (instancetype)initWithMax:(uint32_t)maxType
                    minType:(uint32_t)minType
                   reqClass:(Class)reqClass
                     rspClass:(Class)rspClass
                     pbCore:(ZYPBCore *)core
{
    if (self = [super init]) {
        _maxType = maxType;
        _minType = minType;
        _reqCls = reqClass;
        _rspCls = rspClass;
        _isCanceled = NO;
    }
    return self;
}

- (void)cancel
{
    @synchronized (self) {
        if (_isCanceled) {
            return;
        }
        _isCanceled = YES;
        [self p_removeFromContainer];
    }
}

- (BOOL)isRespondToMaxType:(uint32_t)maxType minType:(uint32_t)minType
{
    //_minType + 1 则是responseClass
    if (maxType == _maxType && (minType == _minType || minType == _minType + 1)) {
        return YES;
    }
    return NO;
}

- (void)p_removeFromContainer
{
    if (self.core) {
        [self.core removePBToken:self ForKey:PBKeyForType(self.maxType, self.minType)];
    }
}
@end


@interface ZYPBNotificationToken()
@property (nonatomic, strong, readwrite) Class rspCls;
@property (nonatomic, assign, readwrite) uint32_t maxType;
@property (nonatomic, assign, readwrite) uint32_t minType;
@property (nonatomic, assign, readwrite) BOOL isCanceled;

@property (nonatomic, weak) ZYPBCore *core;
@end

@implementation ZYPBNotificationToken

- (instancetype)initWithMax:(uint32_t)maxType
                    minType:(uint32_t)minType
                   rspClass:(Class)rspClass
                     pbCore:(ZYPBCore *)core
{
    if (self = [super init]) {
        _maxType = maxType;
        _minType = minType;
        _rspCls = rspClass;
    }
    return self;
}

- (void)cancel
{
    @synchronized (self) {
        if (_isCanceled) {
            return;
        }
        _isCanceled = YES;
        _completedBlock = nil;
        [self p_removeFromContainer];
    }
}

- (BOOL)isRespondToMaxType:(uint32_t)maxType minType:(uint32_t)minType
{
    if (maxType == _maxType && minType == _minType) {
        return YES;
    }
    return NO;
}


- (void)doCompletedOfresponseObj:(nullable id)responseObj error:(nullable NSError *)error;
{
    if ([NSThread isMainThread]) {
        self.completedBlock ? self.completedBlock(responseObj, error) : nil;
    }
    else
    {
        dispatch_async(dispatch_get_main_queue(), ^{
            self.completedBlock ? self.completedBlock(responseObj, error) : nil;
        });
    }
}

- (void)p_removeFromContainer
{
    if (self.core) {
        [self.core removePBToken:self ForKey:PBKeyForType(self.maxType, self.minType)];
    }
}

@end
